package com.endes.android.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.endes.android.entidades.SSEC01;
import com.endes.android.entidades.SSEC02;
import com.endes.android.model.database.EndesOpenHelper;

public class QSSec2Dao {
	private EndesOpenHelper endesOpenHelper;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	public QSSec2Dao(Context context) {
		this.context = context;
		endesOpenHelper = new EndesOpenHelper(context);

	}

	public SSEC02 ObtenerSeccion2(int qsCluster, int qsNumber, int qsHome, 
			int numeroOrdenPersonaSeleccionada) {
		SSEC02 ssec02 = null;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC02 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "", qsHome + "", 
								numeroOrdenPersonaSeleccionada + ""});
		if (cursor.moveToFirst()) {
			ssec02 = new SSEC02();
			ssec02.setQsCluster(qsCluster);
			ssec02.setQsNumber(qsNumber);
			ssec02.setQsHome(qsHome);
			ssec02.setQs20C(numeroOrdenPersonaSeleccionada);
			ssec02.setQs200(cursor.getInt(cursor.getColumnIndex("qs200")));
			ssec02.setQs201(cursor.getInt(cursor.getColumnIndex("qs201")));
			ssec02.setQs202(cursor.getInt(cursor.getColumnIndex("qs202")));
			ssec02.setQs203U(cursor.getInt(cursor.getColumnIndex("qs203U")));
			ssec02.setQs203C(cursor.getInt(cursor.getColumnIndex("qs203C")));
			ssec02.setQs204U(cursor.getInt(cursor.getColumnIndex("qs204U")));
			ssec02.setQs204C(cursor.getInt(cursor.getColumnIndex("qs204C")));
			ssec02.setQs205U(cursor.getInt(cursor.getColumnIndex("qs205U")));
			ssec02.setQs205C(cursor.getInt(cursor.getColumnIndex("qs205C")));
			ssec02.setQs206(cursor.getInt(cursor.getColumnIndex("qs206")));
			ssec02.setQs207U(cursor.getInt(cursor.getColumnIndex("qs207U")));
			ssec02.setQs207C(cursor.getInt(cursor.getColumnIndex("qs207C")));
			ssec02.setQs208(cursor.getInt(cursor.getColumnIndex("qs208")));
			ssec02.setQs209(cursor.getInt(cursor.getColumnIndex("qs209")));
			ssec02.setQs210(cursor.getInt(cursor.getColumnIndex("qs210")));
			ssec02.setQs211U(cursor.getInt(cursor.getColumnIndex("qs211U")));
			ssec02.setQs211C(cursor.getInt(cursor.getColumnIndex("qs211C")));
			ssec02.setQs212A(cursor.getInt(cursor.getColumnIndex("qs212A")));
			ssec02.setQs212AV(cursor.getInt(cursor.getColumnIndex("qs212AV")));
			ssec02.setQs212AB(cursor.getInt(cursor.getColumnIndex("qs212AB")));
			ssec02.setQs212B(cursor.getInt(cursor.getColumnIndex("qs212B")));
			ssec02.setQs212BV(cursor.getInt(cursor.getColumnIndex("qs212BV")));
			ssec02.setQs212BB(cursor.getInt(cursor.getColumnIndex("qs212BB")));
			ssec02.setQs212C(cursor.getInt(cursor.getColumnIndex("qs212C")));
			ssec02.setQs212CV(cursor.getInt(cursor.getColumnIndex("qs212CV")));
			ssec02.setQs212CB(cursor.getInt(cursor.getColumnIndex("qs212CB")));
			ssec02.setQs212D(cursor.getInt(cursor.getColumnIndex("qs212D")));
			ssec02.setQs212DV(cursor.getInt(cursor.getColumnIndex("qs212DV")));
			ssec02.setQs212DB(cursor.getInt(cursor.getColumnIndex("qs212DB")));
			ssec02.setQs212E(cursor.getInt(cursor.getColumnIndex("qs212E")));
			ssec02.setQs212EV(cursor.getInt(cursor.getColumnIndex("qs212EV")));
			ssec02.setQs212EB(cursor.getInt(cursor.getColumnIndex("qs212EB")));
			ssec02.setQs212F(cursor.getInt(cursor.getColumnIndex("qs212F")));
			ssec02.setQs212FV(cursor.getInt(cursor.getColumnIndex("qs212FV")));
			ssec02.setQs212FB(cursor.getInt(cursor.getColumnIndex("qs212FB")));
			ssec02.setQs212G(cursor.getInt(cursor.getColumnIndex("qs212G")));
			ssec02.setQs212GV(cursor.getInt(cursor.getColumnIndex("qs212GV")));
			ssec02.setQs212GB(cursor.getInt(cursor.getColumnIndex("qs212GB")));
			ssec02.setQs212O(cursor.getInt(cursor.getColumnIndex("qs212O")));
			ssec02.setQs212X(cursor.getString(cursor.getColumnIndex("qs212X")));
			ssec02.setQs212XV(cursor.getInt(cursor.getColumnIndex("qs212XV")));
			ssec02.setQs212XB(cursor.getInt(cursor.getColumnIndex("qs212XB")));
			ssec02.setQs212Z(cursor.getInt(cursor.getColumnIndex("qs212Z")));
			ssec02.setQs213U(cursor.getInt(cursor.getColumnIndex("qs213U")));
			ssec02.setQs213C(cursor.getInt(cursor.getColumnIndex("qs213C")));
			ssec02.setQs214U(cursor.getInt(cursor.getColumnIndex("qs214U")));
			ssec02.setQs214C(cursor.getInt(cursor.getColumnIndex("qs214C")));
			ssec02.setQs215U(cursor.getInt(cursor.getColumnIndex("qs215U")));
			ssec02.setQs215C(cursor.getInt(cursor.getColumnIndex("qs215C")));
			ssec02.setQs216U(cursor.getInt(cursor.getColumnIndex("qs216U")));
			ssec02.setQs216C(cursor.getInt(cursor.getColumnIndex("qs216C")));
			ssec02.setQs217U(cursor.getInt(cursor.getColumnIndex("qs217U")));
			ssec02.setQs217C(cursor.getInt(cursor.getColumnIndex("qs217C")));
			ssec02.setQs218U(cursor.getInt(cursor.getColumnIndex("qs218U")));
			ssec02.setQs218C(cursor.getInt(cursor.getColumnIndex("qs218C")));
			ssec02.setQs219U(cursor.getInt(cursor.getColumnIndex("qs219U")));
			ssec02.setQs219C(cursor.getInt(cursor.getColumnIndex("qs219C")));
			ssec02.setQs220U(cursor.getInt(cursor.getColumnIndex("qs220U")));
			ssec02.setQs220CV(cursor.getInt(cursor.getColumnIndex("qs220CV")));
			ssec02.setQs220CC(cursor.getInt(cursor.getColumnIndex("qs220CC")));
		}
		Cerrar();
		return ssec02;
	}

	public void guardarSec2Encuesta(SSEC02 ssec02) {
		ContentValues contenido = new ContentValues();
		contenido.put("qs200", ssec02.getQs200());
		contenido.put("qs201", ssec02.getQs201());
		contenido.put("qs202", ssec02.getQs202());
		contenido.put("qs203U", ssec02.getQs203U());
		contenido.put("qs203C", ssec02.getQs203C());
		contenido.put("qs204U", ssec02.getQs204U());
		contenido.put("qs204C", ssec02.getQs204C());
		contenido.put("qs205U", ssec02.getQs205U());
		contenido.put("qs205C", ssec02.getQs205C());
		contenido.put("qs206", ssec02.getQs206());
		contenido.put("qs207U", ssec02.getQs207U());
		contenido.put("qs207C", ssec02.getQs207C());
		contenido.put("qs208", ssec02.getQs208());
		contenido.put("qs209", ssec02.getQs209());
		contenido.put("qs210", ssec02.getQs210());
		contenido.put("qs211U", ssec02.getQs211U());
		contenido.put("qs211C", ssec02.getQs211C());
		contenido.put("qs212A", ssec02.getQs212A());
		contenido.put("qs212AV", ssec02.getQs212AV());
		contenido.put("qs212AB", ssec02.getQs212AB());
		contenido.put("qs212B", ssec02.getQs212B());
		contenido.put("qs212BV", ssec02.getQs212BV());
		contenido.put("qs212BB", ssec02.getQs212BB());
		contenido.put("qs212C", ssec02.getQs212C());
		contenido.put("qs212CV", ssec02.getQs212CV());
		contenido.put("qs212CB", ssec02.getQs212CB());
		contenido.put("qs212D", ssec02.getQs212D());
		contenido.put("qs212DV", ssec02.getQs212DV());
		contenido.put("qs212DB", ssec02.getQs212DB());
		contenido.put("qs212E", ssec02.getQs212E());
		contenido.put("qs212EV", ssec02.getQs212EV());
		contenido.put("qs212EB", ssec02.getQs212EB());
		contenido.put("qs212F", ssec02.getQs212F());
		contenido.put("qs212FV", ssec02.getQs212FV());
		contenido.put("qs212FB", ssec02.getQs212FB());
		contenido.put("qs212G", ssec02.getQs212G());
		contenido.put("qs212GV", ssec02.getQs212GV());
		contenido.put("qs212GB", ssec02.getQs212GB());
		contenido.put("qs212O", ssec02.getQs212O());
		contenido.put("qs212X", ssec02.getQs212X());
		contenido.put("qs212XV", ssec02.getQs212XV());
		contenido.put("qs212XB", ssec02.getQs212XB());
		contenido.put("qs212Z", ssec02.getQs212Z());
		contenido.put("qs213U", ssec02.getQs213U());
		contenido.put("qs213C", ssec02.getQs213C());
		contenido.put("qs214U", ssec02.getQs214U());
		contenido.put("qs214C", ssec02.getQs214C());
		contenido.put("qs215U", ssec02.getQs215U());
		contenido.put("qs215C", ssec02.getQs215C());
		contenido.put("qs216U", ssec02.getQs216U());
		contenido.put("qs216C", ssec02.getQs216C());
		contenido.put("qs217U", ssec02.getQs217U());
		contenido.put("qs217C", ssec02.getQs217C());
		contenido.put("qs218U", ssec02.getQs218U());
		contenido.put("qs218C", ssec02.getQs218C());
		contenido.put("qs219U", ssec02.getQs219U());
		contenido.put("qs219C", ssec02.getQs219C());
		contenido.put("qs220U", ssec02.getQs220U());
		contenido.put("qs220CV", ssec02.getQs220CV());
		contenido.put("qs220CC", ssec02.getQs220CC());
		if (ExisteSSEC02(ssec02)) {
			db = endesOpenHelper.getWritableDatabase();
			db.update(
					"SSEC02",
					contenido,
					"qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
					new String[] { ssec02.getQsCluster() + "",
							ssec02.getQsNumber() + "", ssec02.getQsHome() + "",
							ssec02.getQs20C() + ""});
			db.close();
		} else {
			db = endesOpenHelper.getWritableDatabase();
			contenido.put("qsCluster", ssec02.getQsCluster());
			contenido.put("qsNumber", ssec02.getQsNumber());
			contenido.put("qsHome", ssec02.getQsHome());
			contenido.put("qs20C", ssec02.getQs20C());
			db.insert("SSEC02", null, contenido);
			db.close();
		}
	}

	public boolean ExisteSSEC02(SSEC02 ssec02) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC02 where qsCluster=? and qsNumber=? and qsHome=? " +
						"and qs20C=?",
						new String[] { ssec02.getQsCluster() + "",
								ssec02.getQsNumber() + "",
								ssec02.getQsHome() + "",
								ssec02.getQs20C() + ""});
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}
	
	public int verificarEdadPregunta23(int qsCluster, int qsNumber, int qsHome,
			int numeroOrdenPersonaSeleccionada){
		int edad = 0;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select qs23 from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "",
								numeroOrdenPersonaSeleccionada + "" });
		if (cursor.moveToFirst()) {
			edad = cursor.getInt(cursor.getColumnIndex("qs23"));
		}
		return edad;
	}

	public void Cerrar() {
		cursor.close();
		db.close();
	}

}
